package com.hyg.module_report.ui.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataModel.ReceiveData;
import com.hyg.lib_common.constant.UrlUtils;
import com.hyg.module_report.R;
import com.hyg.module_report.model.ReportListData;
import com.hyg.module_report.ui.fragment.HealthAdviceV2DetailFragment;
import com.hyg.module_report.ui.fragment.ReportV2DetailFragment;

import java.lang.ref.WeakReference;

import okhttp3.FormBody;

public class HealthReportV2Activity extends BaseActivity {
    ImageView back;
    TextView reportBtn, adviceBtn;
    FragmentManager fragmentManager;
    FragmentTransaction transaction;

    Fragment currentFragment;
    HealthAdviceV2DetailFragment healthAdviceFragment;
    ReportV2DetailFragment reportFragment;

    public String MedicalId = "";
    public ReportListData reportListData;

    CustomHandler customHandler;
    public ReceiveData adviceReceiveData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_report_v2);
        init();
    }

    public void init() {
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 00, null, null, true);
        customHandler = new CustomHandler(this);
        initFragment();
        initView();
        initData();
    }

    public void initFragment() {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        healthAdviceFragment = new HealthAdviceV2DetailFragment();
        reportFragment = new ReportV2DetailFragment();
        transaction.add(R.id.content_ln, reportFragment);
        currentFragment = reportFragment;
        transaction.commit();
    }

    public void initView() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    return;
                }
                finish();
                overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
            }
        });

        reportBtn = findViewById(R.id.user_report_btn);
        adviceBtn = findViewById(R.id.health_advice_btn);
        reportBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(reportFragment);
                reportBtn.setTextColor(getResources().getColor(R.color.MainThemeColor3));
                adviceBtn.setTextColor(getResources().getColor(R.color.main_disabled_stroke_color));
            }
        });

        adviceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(healthAdviceFragment);
                reportBtn.setTextColor(getResources().getColor(R.color.main_disabled_stroke_color));
                adviceBtn.setTextColor(getResources().getColor(R.color.MainThemeColor3));
            }
        });
    }

    public void initData() {
        Intent it = getIntent();
        reportListData = it.getParcelableExtra("MedicalId");
        if (null != reportListData) {
            MedicalId = reportListData.getReport_id();
        }
    }

    /**
     * 展示Fragment
     */
    private void showFragment(Fragment fragment) {
        if (currentFragment != fragment) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.hide(currentFragment);
            currentFragment = fragment;
            if (!fragment.isAdded()) {
                transaction.add(R.id.content_ln, fragment).show(fragment).commit();
            } else {
                transaction.show(fragment).commit();
            }
        }
    }

    /**
     * description: 下载更新养生建议数据
     *
     * @author visitant
     * @time 2021/1/6 11:39
     */
    public void updateAdviceData() {
        getAdviceData(reportListData.getDetailData().getYsIdJson());
    }

    /**
     * description: 养生数据网络请求
     *
     * @author visitant
     * @time 2021/1/6 11:39
     */
    public void getAdviceData(String id) {
        {
            String str_tag = "88";
            FormBody formBody = new FormBody.Builder()
                    .add("ysIdJson", id)//设置参数名称和参数值
                    .build();
            sendPostNetwork(str_tag, formBody, customHandler, UrlUtils.getV2AdviceData);
        }
    }


    private class CustomHandler extends Handler {
        private final WeakReference<Context> reference;
        private HealthReportV2Activity activity;

        public CustomHandler(Context context) {
            reference = new WeakReference<>(context);
            activity = (HealthReportV2Activity) reference.get();
        }

        public void handleMessage(Message msg) {
            adviceReceiveData = JSONObject.parseObject(msg.obj.toString(), ReceiveData.class);
            if (msg.what == 88) {//查询报告data
                if (currentFragment == healthAdviceFragment) {
                    healthAdviceFragment.showHealthAdviceView(adviceReceiveData);
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        customHandler.removeCallbacksAndMessages(null);
    }


}